﻿using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 权限编辑
    /// </summary>
    public partial class WdRoleEditModel : ObservableObject
    {
        /// <summary>
        /// 用户
        /// </summary>
        [ObservableProperty]
        private SysUserEntity userEntity;

        /// <summary>
        /// 菜单
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<WdRoleEditModelItem> menuItem;


        /// <summary>
        /// 权限
        /// </summary>
        SysRoleMenuDal sysRoleMenuDal { get; set; }
        /// <summary>
        /// 菜单
        /// </summary>
        SysMenuDal sysMenuDal { get; set; }
        /// <summary>
        /// 保存
        /// </summary>
        public ICommand Save { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public WdRoleEditModel()
        {
            sysRoleMenuDal = new();
            sysMenuDal = new();
            menuItem = new ObservableCollection<WdRoleEditModelItem>();
            Save = new RelayCommand(SaveMethod);
        }

        private void SaveMethod()
        {
            sysRoleMenuDal.DeleteByUser(UserEntity.UserName);
            List<SysRoleMenuEntity> addData = new List<SysRoleMenuEntity>();
            foreach (WdRoleEditModelItem item in MenuItem)
            {
                if (item.IsCheck == false)
                {
                    continue;
                }
                
                SysRoleMenuEntity entity = new SysRoleMenuEntity()
                {
                    UserName = UserEntity.UserName,
                    UserNo = UserEntity.UserNo,
                    Identity = UserEntity.Identify,
                    MenuId = item.SysMenuEntity.Id,
                    NodeName = item.SysMenuEntity.NodeName,
                };
                addData.Add(entity);

                if (item.Items!=null)
                {
                    foreach (var item2 in item.Items)
                    {
                        if (item2.IsCheck == false)
                        {
                            continue;
                        }
                        SysRoleMenuEntity entity1 = new SysRoleMenuEntity()
                        {
                            UserName = UserEntity.UserName,
                            UserNo = UserEntity.UserNo,
                            Identity = UserEntity.Identify,
                            MenuId = item2.SysMenuEntity.Id,
                            NodeName = item2.SysMenuEntity.NodeName,
                        };
                        addData.Add(entity1);
                    }
                }
            }
            sysRoleMenuDal.AddEntitys(addData);
        }

        public void LoadData()
        {
            MenuItem.Clear();
            var userroles = sysRoleMenuDal.QueryEntitiesByUser(UserEntity.UserName);
            var menus = sysMenuDal.QueryMenuNode();
            var parent = menus.FindAll(x => x.ParentIndex == -1);
            foreach (var menu in parent)
            {
                WdRoleEditModelItem item = new WdRoleEditModelItem()
                {
                    SysMenuEntity = menu,
                    Name = menu.NodeName,
                    Items=new ObservableCollection<WdRoleEditModelItem>()
                };
         

                var child = menus.FindAll(x => x.ParentIndex == menu.BaseIndex);
                foreach (var ch in child)
                {
                    WdRoleEditModelItem item2 = new WdRoleEditModelItem()
                    {
                        SysMenuEntity = ch,
                        Name = ch.NodeName,
                    };
                    item2.IsCheck = userroles.Find(x => x.MenuId == ch.Id) != null;
                    item.Items.Add(item2);
                }
                item.IsCheck = userroles.Find(x => x.MenuId == menu.Id) != null;
                MenuItem.Add(item);
            }

        }
    }

    public partial class WdRoleEditModelItem : ObservableObject
    {
        /// <summary>
        /// 名称
        /// </summary>
        [ObservableProperty]
        private string name;

        /// <summary>
        /// 是否选中
        /// </summary>
        [ObservableProperty]
        private bool isCheck;
        /// <summary>
        /// 菜单
        /// </summary>
        [ObservableProperty]
        private SysMenuEntity sysMenuEntity;

        /// <summary>
        /// 子节点
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<WdRoleEditModelItem> items;
    }
}
